<template>
  <div class="app-container">
    <!-- 搜索栏 -->
    <el-form :inline="true" :model="searchForm" class="mb-20">
      <el-form-item label="关键词">
        <el-input v-model="searchForm.keyword" placeholder="输入关键词" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
        <el-button @click="resetSearch">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- IP 表格 -->
    <el-table :data="ipList" border style="width: 100%">
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          {{ (currentPage - 1) * pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column prop="ipAddress" label="IP 地址" align="center" />
      <el-table-column prop="resourceName" label="资源名称" align="center" />
      <el-table-column prop="isBlack" label="是否黑名单" width="120" align="center">
        <template #default="scope">
          <el-tag :type="scope.row.isBlack === 1 ? 'danger' : 'success'">
            {{ scope.row.isBlack === 1 ? '是' : '否' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="enterTime" label="访问时间" width="160" align="center">
        <template #default="scope">
          {{ formatTime(scope.row.enterTime) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center">
        <template #default="scope">
          <el-button
            v-if="scope.row.isBlack === 1"
            type="success"
            @click="handleUnblockIP(scope.row)"
          >
            移出黑名单
          </el-button>
          <el-button
            v-else
            type="danger"
            @click="handleBlockIP(scope.row)"
          >
            加入黑名单
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination
      class="mt-20"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      :page-sizes="[10, 20, 30, 50]"
      :page-size="pageSize"
      :current-page="currentPage"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>

<script>
import { getList, blockIP, unblockIP } from '@/api/ip'
import moment from 'moment'

export default {
  name: 'Ip',
  data() {
    return {
      ipList: [], // IP 记录列表
      total: 0, // 总记录数
      pageSize: 10, // 每页数量
      currentPage: 1, // 当前页码
      searchForm: {
        keyword: ''
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    // 获取 IP 列表
    async fetchData() {
      try {
        const res = await getList({
          pageNum: this.currentPage,
          pageSize: this.pageSize,
          ...this.searchForm
        })
        this.ipList = res.ipTablePage.records
        this.total = res.ipTablePage.total
      } catch (error) {
        this.$message.error('获取 IP 记录失败')
      }
    },
    // 格式化时间
    formatTime(time) {
      return moment(time).format('YYYY-MM-DD HH:mm:ss')
    },
    // 搜索
    handleSearch() {
      this.currentPage = 1
      this.fetchData()
    },
    // 重置搜索
    resetSearch() {
      this.searchForm = {
        ipAddress: '',
        resourceName: ''
      }
      this.currentPage = 1
      this.fetchData()
    },
    // 分页
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.fetchData()
    },
    // 加入黑名单
    async handleBlockIP(row) {
      try {
        await blockIP({ ipAddress: row.ipAddress })
        this.$message.success('IP 加入黑名单成功')
        row.isBlack = 1 // 前端直接更新状态
      } catch (error) {
        this.$message.error('操作失败')
      }
    },
    // 移出黑名单
    async handleUnblockIP(row) {
      try {
        await unblockIP({ ipAddress: row.ipAddress })
        this.$message.success('IP 移出黑名单成功')
        row.isBlack = 0 // 前端直接更新状态
      } catch (error) {
        this.$message.error('操作失败')
      }
    }
  }
}
</script>

<style scoped>
.mb-20 {
  margin-bottom: 20px;
}

.mt-20 {
  margin-top: 20px;
}
</style>
